{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a506fd71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "200\n",
      "300\n"
     ]
    }
   ],
   "source": [
    "#全局二值化（人工定义阈值）\n",
    "import numpy as np\n",
    "import cv2 as cv\n",
    "\n",
    "img = np.zeros((300,400), np.uint8)\n",
    "#原图\n",
    "step = 100\n",
    "min,max = step,400\n",
    "for s in range(min,max,step):\n",
    "    print(s)\n",
    "    img[:,0+s:min+s] = 40 + 200*s/max\n",
    "    \n",
    "threshold = 127  #人工定义的阈值\n",
    "maxValue = 255   #对全局任意像素，大于max时置为max...\n",
    "thresholdType = cv.THRESH_BINARY\n",
    "#ret二值化阈值 thresh1 二值图\n",
    "ret,thresh1 = cv.threshold(img,threshold,maxValue,thresholdType)\n",
    "\n",
    "cv.imshow('img',img)\n",
    "cv.imshow('thresh1',thresh1)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "524ceff6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
